/*
 * @Copyright: Citylife 2021
 * @Author: shaokui.gu
 * @Date: 2021-02-19 14:55:59
 * @Description: 
 */
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../components/navigation_bar.dart';
import '../utils/progress_hud/progress_hud.dart';
import '../utils/sizer/sizer_ext.dart';
import '../utils/theme/theme_manager.dart';
import 'base_controller.dart';

abstract class BasePage<T extends BaseController> extends GetView<T> {
  /// 背景颜色
  final Color backgroundColor;

  /// 页面是否保活
  final bool keepAlive;

  /// 构造函数
  BasePage({this.backgroundColor, this.keepAlive = false}) : super() {
    Get.put<T>(initController(), permanent: keepAlive);
  }

  /// 初始化controller
  T initController();

  /// loading控件
  Obx _createProgressHud() {
    return Obx(
      () => controller.isFetchingData
          ? Align(
              child: ProgressHUD(
                backgroundColor: Colors.transparent,
                color: theme.colors.toastTextColor,
                containerColor: theme.colors.hudBackgroundColor,
                borderRadius: 8.0.w,
                loading: true,
              ),
            )
          : SizedBox.shrink(),
    );
  }
}

abstract class TemplatePage<T extends TemplateController> extends BasePage<T> {
  /// 背景颜色
  final Color backgroundColor;

  /// 页面标题
  final String title;

  /// 页面是否保活
  final bool keepAlive;

  TemplatePage({this.backgroundColor, this.title, this.keepAlive = false})
      : super(backgroundColor: backgroundColor, keepAlive: keepAlive);

  /// 页面内容
  Widget build(BuildContext context) {
    Widget navigationBar = buildNavigationBar(context);
    List<Widget> children = [];
    if (navigationBar != null) {
      children.add(navigationBar);
    }
    children.add(
      Expanded(
        child: buildBody(context),
      ),
    );

    return Scaffold(
      backgroundColor: backgroundColor ?? theme.colors.pageBackgroundColor,
      body: Stack(
        children: [
          Column(
            children: children,
          ),
          Align(
            alignment: Alignment.center,
            child: _createProgressHud(),
          ),
        ],
      ),
      bottomNavigationBar: buildBottomBar(context),
    );
  }

  /// 页面内容
  Widget buildBody(BuildContext context);

  /// 顶部导航栏
  Widget buildNavigationBar(BuildContext context) {
    return NavigationBar.backBar(
      title: title,
      rightWidgets: navigationBarRightActions(),
    );
  }

  /// 导航栏左侧控件
  List<Widget> navigationBarLeftActions() {
    return null;
  }

  /// 导航栏右侧控件
  List<Widget> navigationBarRightActions() {
    return null;
  }

  /// 底部tabbar
  Widget buildBottomBar(BuildContext context) {
    return null;
  }

  /// 初始化controller
  @override
  T initController();
}
